﻿package cgs.server.logging
{
    import cgs.server.logging.requests.*;
    import cgs.server.logging.responses.*;
    import flash.events.*;
    import flash.net.*;
    import flash.utils.*;

    public class ServerRequestHandler extends Object implements IServerRequestHandler
    {
        private var _requestMap:Dictionary;

        public function ServerRequestHandler()
        {
            this._requestMap = new Dictionary();
            return;
        }// end function

        public function request(param1:IServerRequest) : void
        {
            var urlRequest:URLRequest;
            var urlLoader:URLLoader;
            var callback:Function;
            var extraData:*;
            var serverRequest:* = param1;
            if (serverRequest.isPOST)
            {
                urlRequest = new URLRequest(serverRequest.baseURL);
                urlRequest.method = URLRequestMethod.POST;
                urlRequest.data = serverRequest.urlVariables;
            }
            else
            {
                urlRequest = new URLRequest(serverRequest.url);
                urlRequest.method = URLRequestMethod.GET;
            }
            urlLoader = new URLLoader();
            urlLoader.dataFormat = serverRequest.dataFormat;
            urlLoader.addEventListener(Event.COMPLETE, this.handleRequestComplete);
            urlLoader.addEventListener(IOErrorEvent.IO_ERROR, this.handleRequestIOError);
            urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, this.handleRequestHTTPStatus);
            urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, this.handleRequestSecurityError);
            this._requestMap[urlLoader] = serverRequest;
            try
            {
                urlLoader.load(urlRequest);
            }
            catch (e:Error)
            {
                delete _requestMap[urlLoader];
                callback = serverRequest.callback;
                extraData = serverRequest.extraData;
                if (callback != null)
                {
                    if (extraData != null)
                    {
                        this.callback(null, true, extraData);
                    }
                    else
                    {
                        this.callback(null, true);
                    }
                }
            }
            return;
        }// end function

        private function handleRequestComplete(event:Event) : void
        {
            var _loc_6:IServerResponse = null;
            var _loc_9:* = undefined;
            var _loc_2:* = event.target as URLLoader;
            var _loc_3:* = this._requestMap[_loc_2];
            delete this._requestMap[_loc_2];
            if (_loc_3 == null)
            {
                return;
            }
            var _loc_4:* = _loc_3.dataFormat;
            var _loc_5:* = _loc_3.responseClass;
            var _loc_7:* = _loc_2.data;
            if (_loc_5 != null)
            {
                _loc_6 = new _loc_5;
                _loc_6.data = _loc_7;
                _loc_7 = _loc_6;
            }
            var _loc_8:* = _loc_3.callback;
            if (_loc_3.callback != null)
            {
                _loc_9 = _loc_3.extraData;
                if (_loc_9 == null)
                {
                    this._loc_8(_loc_7, false);
                }
                else
                {
                    this._loc_8(_loc_7, false, _loc_9);
                }
            }
            return;
        }// end function

        private function handleRequestIOError(event:IOErrorEvent) : void
        {
            this.handleRequestError(event.target as URLLoader);
            return;
        }// end function

        private function handleRequestHTTPStatus(event:HTTPStatusEvent) : void
        {
            return;
        }// end function

        private function handleRequestSecurityError(event:SecurityErrorEvent) : void
        {
            this.handleRequestError(event.target as URLLoader);
            return;
        }// end function

        private function handleRequestError(param1:URLLoader) : void
        {
            var _loc_2:* = this._requestMap[param1];
            delete this._requestMap[param1];
            var _loc_3:* = _loc_2.callback;
            var _loc_4:* = _loc_2.extraData;
            if (_loc_3 != null)
            {
                if (_loc_4 == null)
                {
                    this._loc_3(null, true);
                }
                else
                {
                    this._loc_3(null, true, _loc_4);
                }
            }
            return;
        }// end function

    }
}
